/*
' Copyright (c) 2011  DotNetNuke Corporation
'  All rights reserved.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
'
*/

using System;
using System.Linq;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Exceptions;

namespace DotNetNuke.Modules._2DoTasks
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Settings class manages Module Settings
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class SettingsProjectList : _2DoTasksSettingsBase
    {
        public const string DefaultRole = "Administrators";

        #region Base Method Implementations

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// LoadSettings loads the settings from the Database and displays them
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void LoadSettings()
        {
            try
            {
                if (Page.IsPostBack == false)
                {
                    FillControls();
                    string roleName = DefaultRole;

                    /* uncomment to load saved settings in the text boxes*/
                    if (Settings.Contains("MemberRole") &&
                        drp_MemberRole.Items.OfType<ListItem>()
                        .Any(p => p.Value == Settings["MemberRole"].ToString()))
                    {
                        roleName = Settings["MemberRole"].ToString();
                    }

                    var role = drp_MemberRole.Items.FindByValue(roleName);
                    if (role != null)
                        role.Selected = true;
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// UpdateSettings saves the modified settings to the Database
        /// </summary>
        /// -----------------------------------------------------------------------------
        public override void UpdateSettings()
        {
            try
            {
                ModuleController modules = new ModuleController();

                //the following are two sample Module Settings, using the text boxes that are commented out in the ASCX file.
                modules.UpdateModuleSetting(ModuleId, "MemberRole", drp_MemberRole.SelectedValue);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion Base Method Implementations

        #region "Aux Functions"

        /// <summary>
        /// Fills base controls
        /// </summary>
        private void FillControls()
        {
            //fill the available portal roles
            var roles =
                new RoleController().GetPortalRoles(PortalId)
                .OfType<RoleInfo>()
                .Select(p => new
            {
                Id = p.RoleID,
                Name = p.RoleName
            }).OrderBy(p => p.Name);

            drp_MemberRole.DataSource = roles;
            drp_MemberRole.DataValueField = "Id";
            drp_MemberRole.DataTextField = "Name";
            drp_MemberRole.DataBind();
        }

        #endregion "Aux Functions"
    }
}